package orbitPrototype.gui;

import orbitPrototype.Util;
import orbitPrototype.loaders.XMLLoader;
import orbitPrototype.objects.DisplayObjectContainer;
import orbitPrototype.objects.Sprite;

import org.w3c.dom.Element;

/**
 * Description:	Base of all GUIWindows. Created from base XMLElement which contains all its subelements.
 * 				The standard attribute in the XMLElement decides whether the GUIWindow will have
 * 				standardized background and header which is defined by a GUIStyle.
 * 
 * @author Drapan
 *
 */
public class GUIWindow extends GUIElement
{		
	private String _windowName = "";
	private String _windowTitle = "";
	
	private GUIStyle _style = null;
	private boolean _standard = false;
	
	private DisplayObjectContainer _frameComponents = null;

	public GUIWindow(int x, int y, Element xmlElement)
	{
		super(x, y, Integer.parseInt(XMLLoader.getElementValue("width", xmlElement)), Integer.parseInt(XMLLoader.getElementValue("height", xmlElement)), null);
		
		_style = new GUIStyle(XMLLoader.getElementValue("style", xmlElement));
		_windowName = xmlElement.getAttribute("uniqueName");
		_windowTitle = xmlElement.getAttribute("uniqueName").toUpperCase();
		_standard = XMLLoader.getAttributeBooleanValue("standard", xmlElement);
		
		if(_standard)
		{
			initStandard();
			this.addChild(new GUIXMLContainer(_style.getWindowFrameDimension(), _style.getWindowHeaderHeight(), this.getWidth() - (_style.getWindowFrameDimension() * 2), this.getHeight() - (_style.getWindowFrameDimension() + _style.getWindowHeaderHeight()), xmlElement, this));
		}
		else
		{
			this.addChild(new GUIXMLContainer(0, 0, this.getWidth(), this.getHeight(), xmlElement, this));
		}
	}
	
	/**
	 * Description:	Add standard elements (background and header) to all standard GUIWindow's
	 */
	private void initStandard()
	{		
		/*
		Element xmlElement = (Element) XMLLoader.getXML("NewFile").getElementsByTagName("Standard").item(0);
			
		addShapes(xmlElement);
		addButtons(xmlElement, false);
		
		EventHandler.registerEventListener(GUISimpleButton.ACTION_LABEL_CLOSE, this, "toggleVisible", true);
		*/
		
		addFrame();
	}
	
	/**
	 * Description:	Adds a standard frame to this window, based on current style.
	 */
	private void addFrame()
	{
		_frameComponents = new DisplayObjectContainer(this.getWidth(), this.getHeight());
		
		int edgeDimension = this.getStyle().getWindowFrameDimension();
		int headerHeight = this.getStyle().getWindowHeaderHeight();
		int headerSideWidth = this.getStyle().getWindowHeaderSideWidth();

		// Header left edge
		_frameComponents.addChild(new Sprite(0, 0, headerSideWidth, headerHeight, 0, "gui", this.getStyle().getName() + "_window_header_side_l"));	
		// Header right edge
		_frameComponents.addChild(new Sprite(this.getWidth() - headerSideWidth, 0, headerSideWidth, headerHeight, 0, "gui", this.getStyle().getName() + "_window_header_side_r"));
		
		// Header fill
		_frameComponents.addChild(new Sprite(headerSideWidth, 0, this.getWidth() - (headerSideWidth * 2), headerHeight, 0, "gui", this.getStyle().getName() + "_window_header"));
		// Header title
		GUIText headerTitle = new GUIText(headerSideWidth, 0, _windowTitle, "left", this);
		headerTitle.setY((headerHeight / 2) - (headerTitle.getHeight() / 2));
		_frameComponents.addChild(headerTitle);
		
		// Lower right corner
		_frameComponents.addChild(new Sprite(this.getWidth() - edgeDimension, this.getHeight() - edgeDimension, 0, "gui", this.getStyle().getName() + "_window_corner"));		
		// Lower left corner
		_frameComponents.addChild(new Sprite(0, this.getHeight() - edgeDimension, Math.PI/2, "gui", _style.getName() + "_window_corner"));		
		
		// Lower edge
		_frameComponents.addChild(new Sprite(edgeDimension, this.getHeight() - edgeDimension, this.getWidth() - (edgeDimension * 2), edgeDimension, 0, "gui", this.getStyle().getName() + "_window_edge_h"));		
		// Right edge
		_frameComponents.addChild(new Sprite(this.getWidth() - edgeDimension, headerHeight, edgeDimension, this.getHeight() - (edgeDimension + headerHeight), 0, "gui", this.getStyle().getName() + "_window_edge_v"));
		// Left edge
		_frameComponents.addChild(new Sprite(0, headerHeight, edgeDimension, this.getHeight() - (edgeDimension + headerHeight), Math.PI, "gui", this.getStyle().getName() + "_window_edge_v"));
		
		// Fill
		_frameComponents.addChild(new Sprite(edgeDimension, headerHeight, this.getWidth() - (edgeDimension * 2), this.getHeight() - (edgeDimension + headerHeight), 0, "gui", this.getStyle().getName() + "_window_fill"));
		
		this.addChild(_frameComponents);
		
		this.addChild(new GUIWindowDragArea(0, 0, this.getWidth(), headerHeight, "move", false, this));
	}
	
	public GUIStyle getStyle()
	{
		return _style;
	}
	
	public String getWindowName()
	{
		return _windowName;
	}
		
	public void toggleVisible()
	{
		this.setVisible(!this.getVisible());
	}
	
	public void move()
	{
		Util.output("boom");
	}
	
	public void destroy()
	{
		
	}
}
